//
//  ComplaintAdviceService.m
//  homeland
//
//  Created by caesar on 14-8-10.
//  Copyright (c) 2014年 xzysun. All rights reserved.
//  社情及时报

#import "ComplaintAdviceService.h"

#import "UIImageView+WebCache.h"
#import "ComplaintAdviceEntity.h"
#import "CommonDataService.h"
#import "ComplaintAdviceItemCellTableViewCell.h"
@implementation ComplaintAdviceService
//请求参数集
NSMutableDictionary *params;

- (void)loadDataWithKey:(NSString *)keyOrNil{
    _currentPageNo = 1;
    if (self.dataList) {
        self.dataList = nil;
    }
    
    _dataItemName = @"ComplaintAdviceEntity";
    params = [NSMutableDictionary dictionary];
 
    if (keyOrNil) {
        [params setValue:keyOrNil forKey:@"title"];
    } else {
        [params setValue:@"" forKey:@"title"];
    }
    [self doHttpPost:SERVER_URL_COMPLAINTADVICE_LIST headers:nil path:[NSString stringWithFormat:@"/%d",_currentPageNo] parameters:params];
}

- (void)loadMoreDataWithKey:(NSString *)keyOrNil{
    if (!params || [params count]==0) {
        params = [NSMutableDictionary dictionary];
    }
    if (keyOrNil) {
        [params setValue:keyOrNil forKey:@"title"];
    } else {
        [params setValue:@"" forKey:@"title"];
    }
    
    _dataItemName = @"ComplaintAdviceEntity";
    [self doHttpPost:SERVER_URL_COMPLAINTADVICE_LIST headers:nil path:[NSString stringWithFormat:@"/%d",_currentPageNo + 1] parameters:params];
}

- (CGFloat)heightForCellAtRow:(NSIndexPath *)index{
    return 90.0f;//民情调查每行88
}

/**展示数据*/
- (void)configCell:(UITableViewCell *)cell atIndex:(NSIndexPath *)index{
    if (self.dataList == nil || self.dataList.count == 0) {
        return;
    }
    //民情调查
    ComplaintAdviceEntity *item = [self.dataList objectAtIndex:index.row];
    ComplaintAdviceItemCellTableViewCell *tempCell = (ComplaintAdviceItemCellTableViewCell *)cell;
    tempCell.titleLabel.text = [CommonUtil dealNullObject:item.title];
    tempCell.detailInfoView.text = [CommonUtil dealNullObject:item.content];
    
    //图片
    NSString *pic = [NSString stringWithFormat:@"http://%@%@", SERVER_URL_COMPLAINTADVICE_MY_NEWSPAPERS_IMAGE, item.imageUrl];
    
    __block UIActivityIndicatorView *activityIndicator;
    __weak UIImageView *weakImageView = tempCell.imageView;
    [tempCell.imageView setImageWithURL:[NSURL URLWithString:pic] placeholderImage:[UIImage imageNamed:@"placeholder.png"] options:SDWebImageProgressiveDownload|SDWebImageRetryFailed|SDWebImageLowPriority progress:^(NSUInteger receivedSize, long long expectedSize)
     {
         if (!activityIndicator)
         {
             [weakImageView addSubview:activityIndicator = [UIActivityIndicatorView.alloc initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]];
             activityIndicator.center = weakImageView.center;
             [activityIndicator startAnimating];
         }
     } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType)
     {
         [activityIndicator removeFromSuperview];
         activityIndicator = nil;
     }];
}

@end
